﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RockCompany.Common.Lib;
using RockCompany.Master.DTO;
using RockCompany.Common.Constant;
using System.Windows.Forms;
using System.Data;

namespace RockCompany.Master.Model
{
    public class SI02AFormModel : CommonModel
    {
        #region Properties
        private DataTable _newMatOredrDetails;
        public DataTable newMatOredrDetails
        {
            get { return _newMatOredrDetails; }
            set { _newMatOredrDetails = value; }
        }
        #endregion

        #region Constructor
        public SI02AFormModel()
        {
            dbConnector = new DBConnector();
            DataList = new List<object>();
        }
        #endregion

        #region Utility Methods
        public DataRow getCheckedRow(int index)
        {
            DataRow curRow = newMatOredrDetails.Rows[index];
            curRow.SetField<bool>(PHIEU_LANH_NVL_CT_DTO.N_CHON, !curRow.Field<bool>(PHIEU_LANH_NVL_CT_DTO.N_CHON));
            return curRow;
        }

        private DataTable searchWorkOrderDetail(List<string> sWorkOrderList)
        {
            DataTable tempDB = null;
            if (sWorkOrderList.Count > 0)
            {
                string sSql = "SELECT " + CONG_LENH_SX_CT.N_MA_NVL_VTU + "," + CONG_LENH_SX_CT.N_SO_LO + "," + CONG_LENH_SX_CT.N_TRONG_LUONG;
                sSql += " FROM " + CONG_LENH_SX_CT.N_TABLE;
                sSql += " WHERE " + CONG_LENH_SX_CT.N_SO_LO + " IN (";
                foreach (string s in sWorkOrderList)
                {
                    sSql += "'" + s + "',";
                }
                sSql = sSql.Substring(0, sSql.Length - 1) + ")";
                tempDB = selectData(sSql);
            }
            return tempDB;
        }
        #endregion

        #region Main Methods
        public bool addNewMatOrderDetail(string sUserName, DataTable dbData)
        {
            //if (dbData != null && dbData.Rows.Count > 0)
            //{
            //    foreach (DataRow curRow in dbData.Rows)
            //    {
            //        DataRow newRow = newMatOredrDetails.NewRow();
            //        newRow.SetField<bool>(PHIEU_LANH_NVL_CT_DTO.N_CHON, false);
            //        newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU, curRow.Field<string>(NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU));
            //        newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_SO_LO, curRow.Field<string>(CONG_LENH_SX_CT.N_SO_LO));
            //        newRow.SetField<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH, curRow.Field<decimal>(CONG_LENH_SX_CT.N_TRONG_LUONG));
            //        newRow.SetField<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH, 0);
            //        newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU, "");
            //        newRow.SetField<DateTime>(PHIEU_LANH_NVL_CT_DTO.N_CREATION_TIME, DateTime.Now);
            //        newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_CREATION_USER, sUserName);
            //        newMatOredrDetails.Rows.Add(newRow);
            //    }
            //}
            //return tempDB != null && tempDB.Rows.Count > 0;
            return false;
        }

        public bool addNewMatOrderDetail(string sUserName, List<string> sWorkOrderList)
        {
            DataTable tempDB = searchWorkOrderDetail(sWorkOrderList);
            if (tempDB != null && tempDB.Rows.Count > 0)
            {
                foreach (DataRow curRow in tempDB.Rows)
                {
                    DataRow newRow = newMatOredrDetails.NewRow();
                    newRow.SetField<bool>(PHIEU_LANH_NVL_CT_DTO.N_CHON, false);
                    newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU, curRow.Field<string>(CONG_LENH_SX_CT.N_MA_NVL_VTU));
                    newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_SO_LO, curRow.Field<string>(CONG_LENH_SX_CT.N_SO_LO));
                    newRow.SetField<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH, curRow.Field<decimal>(CONG_LENH_SX_CT.N_TRONG_LUONG));
                    newRow.SetField<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH, 0);
                    newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU, "");
                    newRow.SetField<DateTime>(PHIEU_LANH_NVL_CT_DTO.N_CREATION_TIME, DateTime.Now);
                    newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_CREATION_USER, sUserName);
                    newMatOredrDetails.Rows.Add(newRow);
                }
            }
            return tempDB != null && tempDB.Rows.Count > 0;
        }

        public bool addNewNoneMatList(string sUserName, DataTable sNoneMatList)
        {
            int add = 0;
            foreach (DataRow curRow in sNoneMatList.Rows)
            {
                DataRow newRow = newMatOredrDetails.NewRow();
                newRow.SetField<bool>(PHIEU_LANH_NVL_CT_DTO.N_CHON, false);
                newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU, curRow.Field<string>(NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU));
                newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_SO_LO, string.Empty);
                newRow.SetField<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH, 0);
                newRow.SetField<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH, 0);
                newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU, "");
                newRow.SetField<DateTime>(PHIEU_LANH_NVL_CT_DTO.N_CREATION_TIME, DateTime.Now);
                newRow.SetField<string>(PHIEU_LANH_NVL_CT_DTO.N_CREATION_USER, sUserName);
                newMatOredrDetails.Rows.Add(newRow);
                add++;
            }
            return add == sNoneMatList.Rows.Count;
        }

        public bool removeMatOredrDetail(string sMatOrderNo)
        {
            string sWhereClause = " WHERE ";
            sWhereClause += PHIEU_LANH_NVL_CT_DTO.N_MS_PHIEU_LANH_NVL + " ='" + sMatOrderNo + "' AND ";
            sWhereClause += PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU + " IN(";
            Dictionary<string, object> pv = new Dictionary<string, object>();
            foreach (DataRow curRow in DataList)
            {
                if (curRow.RowState != DataRowState.Added)
                {
                    string sP = ParaConst.P_MA_NVL_VTU + DataList.IndexOf(curRow).ToString();
                    sWhereClause += sP + ",";
                    pv.Add(sP, curRow.Field<string>(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU));
                }
                newMatOredrDetails.Rows.Remove(curRow);
            }
            DataList.Clear();
            if (pv.Count > 0)
            {
                sWhereClause = sWhereClause.Substring(0, sWhereClause.Length - 1) + ")";
                if (removeRow(PHIEU_LANH_NVL_CT_DTO.N_TABLE, sWhereClause, pv))
                    pv.Clear();
            }
            return pv.Count == 0;
        }

        public bool insertNewMatOrder(PHIEU_LANH_NVL_DTO curMatOrder)
        {
            Dictionary<string, object> pv = new Dictionary<string, object>();
            pv.Add(PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL, curMatOrder.MS_PHIEU_LANH_NVL);
            pv.Add(PHIEU_LANH_NVL_DTO.N_NGAY_LANH_NVL, curMatOrder.NGAY_LANH_NVL);
            pv.Add(PHIEU_LANH_NVL_DTO.N_NGUOI_LANH_NVL, curMatOrder.NGUOI_LANH_NVL);
            pv.Add(PHIEU_LANH_NVL_DTO.N_THU_KHO, curMatOrder.THU_KHO);
            pv.Add(PHIEU_LANH_NVL_DTO.N_PHE_DUYET, curMatOrder.PHE_DUYET);

            pv.Add(PHIEU_LANH_NVL_DTO.N_CREATION_TIME, DateTime.Now);
            pv.Add(PHIEU_LANH_NVL_DTO.N_CREATION_PROGRAM_ID, "SI02A");
            pv.Add(PHIEU_LANH_NVL_DTO.N_CREATION_USER, curMatOrder.CREATION_USER);
            return insertData(PHIEU_LANH_NVL_DTO.N_TABLE, pv);
        }

        private bool insertNewMatOrderDetail(string sCreator, string sMatOrderNo, DataRow newRow)
        {
            Dictionary<string, object> pv = new Dictionary<string, object>();
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_MS_PHIEU_LANH_NVL, sMatOrderNo);
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU, newRow.Field<string>(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU));
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_SO_LO, newRow.Field<string>(PHIEU_LANH_NVL_CT_DTO.N_SO_LO));
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH, newRow.Field<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH));
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH, newRow.Field<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH));
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU, newRow.Field<string>(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU));

            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_CREATION_PROGRAM_ID, "SI02A");
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_CREATION_TIME, DateTime.Now);
            pv.Add(PHIEU_LANH_NVL_CT_DTO.N_CREATION_USER, sCreator);
            return insertData(PHIEU_LANH_NVL_CT_DTO.N_TABLE, pv);
        }

        public bool updateMatOrder(PHIEU_LANH_NVL_DTO curMatOrder)
        {
            string sSetClause = " SET ";
            sSetClause += PHIEU_LANH_NVL_DTO.N_PHE_DUYET + " = @" + PHIEU_LANH_NVL_DTO.N_PHE_DUYET + ",";
            sSetClause += PHIEU_LANH_NVL_DTO.N_NGUOI_LANH_NVL + " = @" + PHIEU_LANH_NVL_DTO.N_NGUOI_LANH_NVL + ",";
            sSetClause += PHIEU_LANH_NVL_DTO.N_THU_KHO + " = @" + PHIEU_LANH_NVL_DTO.N_THU_KHO + ",";
            sSetClause += PHIEU_LANH_NVL_DTO.N_NGAY_LANH_NVL + " = @" + PHIEU_LANH_NVL_DTO.N_NGAY_LANH_NVL;

            string sClause = " WHERE ";
            sClause += PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL + " = @" + PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL;
            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add("@" + PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL, curMatOrder.MS_PHIEU_LANH_NVL);
            updateValues.Add("@" + PHIEU_LANH_NVL_DTO.N_PHE_DUYET, curMatOrder.PHE_DUYET);
            updateValues.Add("@" + PHIEU_LANH_NVL_DTO.N_NGUOI_LANH_NVL, curMatOrder.NGUOI_LANH_NVL);
            updateValues.Add("@" + PHIEU_LANH_NVL_DTO.N_THU_KHO, curMatOrder.THU_KHO);
            updateValues.Add("@" + PHIEU_LANH_NVL_DTO.N_NGAY_LANH_NVL, curMatOrder.NGAY_LANH_NVL);

            updateValues.Add(ParaConst.P_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add(ParaConst.P_LAST_UPDATE_PROGRAM_ID, "SI02A");
            updateValues.Add(ParaConst.P_LAST_UPDATE_USER, curMatOrder.CREATION_USER);
            return updateData(PHIEU_LANH_NVL_DTO.N_TABLE, sSetClause, updateValues, sClause);
        }

        private bool updateMatCheckDetail(string sUpdater, string sMatOrder, DataRow modRow)
        {
            string sSetClause = " SET ";
            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU + " = @" + PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU + ",";
            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_SO_LO + " = @" + PHIEU_LANH_NVL_CT_DTO.N_SO_LO + ",";
            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH + " = @" + PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH + ",";
            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH + " = @" + PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH + ",";
            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU + " = @" + PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU + ",";

            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_TIME + " = @" + PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_TIME + ",\n";
            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_PROGRAM_ID + " = @" + PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_PROGRAM_ID + ",\n";
            sSetClause += PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_USER + " = @" + PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_USER + "\n";

            string sClause = "WHERE ";
            sClause += PHIEU_LANH_NVL_CT_DTO.N_MS_PHIEU_LANH_NVL + " = @" + PHIEU_LANH_NVL_CT_DTO.N_MS_PHIEU_LANH_NVL;
            sClause += " AND " + PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU + " = @" + PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU;

            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_MS_PHIEU_LANH_NVL, sMatOrder);
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU, modRow.Field<string>(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU));
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_SO_LO, modRow.Field<string>(PHIEU_LANH_NVL_CT_DTO.N_SO_LO));
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH, modRow.Field<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH));
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH, modRow.Field<decimal>(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH));
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU, modRow.Field<string>(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU));
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_PROGRAM_ID, "SI02A");
            updateValues.Add("@" + PHIEU_LANH_NVL_CT_DTO.N_LAST_UPDATE_USER, sUpdater);
            return updateData(PHIEU_LANH_NVL_CT_DTO.N_TABLE, sSetClause, updateValues, sClause);
        }

        public bool saveData(string sMatOrderNo, string sUserName)
        {
            DataTable tempDb = null;
            int insertRes = 0, updateRes = 0;

            tempDb = newMatOredrDetails.GetChanges(DataRowState.Modified);
            int updatedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = newMatOredrDetails.GetChanges(DataRowState.Added);
            int addedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = newMatOredrDetails.GetChanges();
            if (tempDb != null && tempDb.Rows.Count > 0)
            {
                foreach (DataRow curRow in tempDb.Rows)
                {
                    switch (curRow.RowState)
                    {
                        case DataRowState.Added:
                            if (insertNewMatOrderDetail(sUserName, sMatOrderNo, curRow))
                                insertRes++;
                            break;
                        case DataRowState.Modified:
                            if (updateMatCheckDetail(sUserName, sMatOrderNo, curRow))
                                updateRes++;
                            break;
                    }
                    newMatOredrDetails.AcceptChanges();
                }
            }
            return insertRes == addedRowCount && updateRes == updatedRowCount;
        }
        #endregion
    }
}
